import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:seedfour/app/models/file_list_entity.dart';

import '../../constants.dart';
import '../../theme.dart';
import 'logic.dart';

class FileListPage extends StatelessWidget {
  FileListPage({Key? key}) : super(key: key);

  final logic = Get.find<FileListLogic>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(logic.title),
        leading: IconButton(
          icon: Icon(
            Icons.arrow_back_ios,
            size: 16.w,
          ),
          onPressed: () {
            Get.back();
          },
        ),
      ),
      body: _buildBg(children: [
        _buildFileList(),
      ]),
    );
  }

  Widget _buildListItem(FileData item) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        SvgPicture.asset('assets/images/file.svg',
            width: kIconSizeMedium, height: kIconSizeMedium),
        SizedBox(width: kSpaceSmall),
        Expanded(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Text(
                item.name ?? '',
                maxLines: 3,
                overflow: TextOverflow.ellipsis,
                style: const TextStyle(fontWeight: AppFont.fontSemiBold),
              ),
              SizedBox(height: kSpaceTiny),
              Text(
                  DateFormat('yyyy-MM-dd HH:mm').format(
                      DateTime.fromMillisecondsSinceEpoch(item.createdAt!)),
                  style:
                      TextStyle(color: Colors.grey, fontSize: kFontSizeMini)),
            ],
          ),
        ),
      ],
    );
  }

  Widget _renderRow(BuildContext context, int index) {
    if (index < logic.files.length) {
      return InkWell(
        onTap: () => {},
        child: Padding(
          padding: EdgeInsets.all(kSpaceSmall),
          child: _buildListItem(logic.files[index]),
        ),
      );
    } else {
      return _getMoreWidget();
    }
  }

  Widget _getMoreWidget() {
    return Center(
      child: Padding(
        padding: EdgeInsets.all(kSpaceMedium),
        child: Text(
          logic.bottomTips.value,
          style: TextStyle(fontSize: kFontSizeSmall, color: Colors.grey),
        ),
      ),
    );
  }

  Widget _buildFileList() {
    return Expanded(
      child: Ink(
        decoration: const BoxDecoration(
          color: Colors.white,
        ),
        child: Obx(
          () => ListView.separated(
              controller: logic.scrollController,
              itemBuilder: _renderRow,
              separatorBuilder: (BuildContext context, int index) =>
                  const Divider(),
              itemCount: logic.files.length + 1),
        ),
      ),
    );
  }

  Widget _buildBg({required List<Widget> children}) {
    return Obx(() {
      if (logic.isLoading.value) {
        return const Center(child: CircularProgressIndicator());
      } else {
        return Column(
          children: children,
        );
      }
    });
  }
}
